package com.txc.cloud.admin.controller;

import com.txc.cloud.admin.model.dto.RoleDto;
import com.txc.cloud.admin.model.vo.RoleVo;
import com.txc.cloud.admin.service.IRoleService;
import com.txc.common.global.result.Result;
import com.txc.common.global.result.ResultCode;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 角色 前端控制器
 *
 * @ClassName: RoleController
 * @Author: TXC
 * @Date: 2022-08-15 15:31
 **/
@RestController
@RequestMapping("/role")
public class RoleController {
    @Resource
    private IRoleService roleService;

    @PostMapping("/add")
    public Result<?> createRole(@RequestBody RoleDto role) {
        roleService.createRole(role);
        return Result.ok();
    }

    @PostMapping("/edit")
    public Result<?> editRole(@RequestBody RoleDto role) {
        roleService.updateRole(role);
        return Result.ok();
    }

    @PostMapping("/empower/{id}")
    public Result<?> empower(@PathVariable Long id, @RequestBody List<String> permissionCodes) {
        boolean flag = roleService.empower(id, permissionCodes);
        if (flag) {
            return Result.ok();
        }
        return Result.fail(ResultCode.ERROR);
    }

    @GetMapping("/list")
    public Result<List<RoleVo>> getRoleList() {
        return Result.ok(roleService.getRoleList());
    }

    @GetMapping("/del/{id}")
    public Result<?> getRoleDetail(@PathVariable Long id) {
        return Result.ok(roleService.removeById(id));
    }
}
